/**
 * 初始化布局UI
 */
// var HttpUrl="http://192.168.98.10:10071";//高杰
// var HttpUrl="http://192.168.99.97:10602";//王亚洲
// var HttpUrl="https://bms.jinwei.net";
var HttpUrl="http://192.168.99.202:10071";
var PreviewUrl="http://192.168.99.201:81/bigscreen/#/";

function frameInit(districtId,parkId,token,districtName) {
    initSelectArea();
    initDrag();
    componentInit();
    initApp(districtId,parkId,token,districtName);
    initLayout();
    document.addEventListener('click', function () {
        app.popup.show = false;
    });
	getServerCarData();//获取后台录入的车位数据0618
	getServerHouseUnit();//获取后台录入的楼号和单元号
	getUploadMapData();//获取之前上传的车位平面图数据06-18
}
function pushShape(data) {
    data.id = getShapeId();
    data.selectArea = false;
    var defaultData = {
        borderWidth: 0,
        borderColor: '#000',
        borderStyle: 'solid',
        fontColor: '#000',
        fontSize: 12,
        fontStyle: '',
        value: '文本标签',
    }
    for (var key in defaultData) {
        if (!data[key]) {
            data[key] = defaultData[key];
        }
    }
    app.shapes.push(data);
}

/**
 * 获取一个随机数id，18位
 * @returns {string}
 */
function getShapeId() {
    return new Date().getTime() + "" + Math.floor(Math.random() * 89999 + 10000);
}

function initSelectArea() {
    Vue.directive('selectarea', {
        inserted(el, binding) {
            new SelectArea({
                onBegin: function (el, e) {
                    
                },
                onHandler: function (data) {
                    var count = 0;
                    var tempShape = null;
                    app.shapes.forEach(item => {
                        //计算出每个item的x1 x2 y1 y2
                        var ix1 = item.left, ix2 = item.left + item.width;
                        var iy1 = item.top, iy2 = item.top + item.height;
                        //碰撞检测
                        var crash = (data.y2 >= iy1 && data.y1 <= iy2) && (data.x2 >= ix1 && data.x1 <= ix2);
                        item.active = crash;
                        item.selectArea = crash;
                        if (crash) {
                            tempShape = item;
                            count++;
                        }
                    });
                    //如果只有一个，显示置顶和底菜单，并显示属性
                    var single = count == 1;
                    for (var i = 1; i < app.popup.menus.length; i++) {
                        app.popup.menus[i].show = single;
                    }
                    if (single) {
                        app.selectShape(null, tempShape);
                    } else {
                        app.selected = {}
                    }
                },
                onEnd: function () {

                }
            }).register(el);
        }
    });
}
function initDrag() {
    Vue.directive('drag', {
        inserted(dv, binding) {
            var drag = new Drag({
                onBegin: function (data) {
                    var id = parseInt(dv.getAttribute('data-id'));
                    var self = {}
                    for (var i = 0; i < app.shapes.length; i++) {
                        var item = app.shapes[i];
                        if (item.id == id) {
                            self = item;
                            break;
                        }
                    }
                    self.active = true;
                    if (!self.selectArea) {
                        app.shapes.forEach(item => {
                            if (item != self) {
                                item.selectArea = false;
                                item.active = false;
                            }
                        })
                    } else {
                        //所有选中的 记录下旧的坐标
                        app.shapes.forEach(item => {
                            if (item.active) {
                                item.oldLeft = item.left;
                                item.oldTop = item.top;
                            }
                        });
                    }
                },
                onEnd: function (data) {
                    app.shapes.forEach(item => {
                        if (item.active) {
                            item.oldLeft = item.left;
                            item.oldTop = item.top;
                        }
                    });
                },
                onDrag: function (data) {
                    var shape = binding.value.shape;
                    shape.left = data.left;
                    shape.top = data.top;

                    //所有选中的移动，不包括当前
                    app.shapes.forEach(item => {
                        if (item.active && item != shape) {
                            item.left = data.x + item.oldLeft;
                            item.top = data.y + item.oldTop;
                        }
                    });
                }
            })
            drag.register(dv);
            //注册拉伸
            var resize = new Resize({
                onResize: function (data) {
                    for (var key in data) {
                        binding.value.shape[key] = data[key];
                    }
                }
            });
            resize.register(dv,binding.value.shape);
        }
    });
}

function moveLeft(val) {
    app.shapes.forEach(item => {
        if (item.active) {
            item.left += val;
			app.selected=item;
        }
    });
}

function moveTop(val) {
    app.shapes.forEach(item => {
        if (item.active) {
            item.top += val;
			app.selected=item;
        }
    });
}

function initApp(mdistrictId,mparkId,mtoken,dName) {
	myConsoleLog('districtId='+mdistrictId+',parkId='+mparkId+',token='+mtoken+'name='+dName);
    window.app = new Vue({
        el: '#app',
        data: {
            layout: {
				background:'',
				//background:'url(http://5b0988e595225.cdn.sohucs.com/images/20190923/1e9f7a6c76334f22a31495a37c580ef0.jpeg)',
                width: getSize().width,
                height: getSize().height,
				scale:1,
				mapName:'',//url参数传入
				left:0
            },
            card: {
                width: 1000,
                height: 710,
				width0: 1000,
				height0: 710,
                name: '',
				price:1
            },
            selected: {},
            isSelected: false,
            shapes: [],
            popup: {
                show: false,
                left: 0,
                top: 0,
                menus: [{
                    text: '删除',
                    icon: 'fa-unlink',
                    show: true,
                    handler: function () {
      //                   if (confirm('您确定要删除选中的元素吗？')) {
						// 	myConsoleLog(app.popup.data);
      //                   }else{
						// 	myConsoleLog(app.selected.name)
						// }
						//删除元素不需要用户再次确认了
						app.shapes.forEach(item=>{
							if(item.name==app.popup.data.name){
								item.show=false;
								app.popup.data.show=false;
							}
						})
						app.cardata.forEach(item=>{
							item.active = false;
							if(item.name==app.popup.data.name){
								item.show=false;
							}
						});
						app.selected = {};
                    }
                }, {
                    split: true,
                    show: true
                }]
            },
            isShiftDown: false,
			cardata:[//通过接口从服务器端获取的后台录入的车位数据
			],
			selectCarName:'',
			selectElementName:'',
			houseData:[],//小区的楼号数据
			selHouse:1,
			selHouseIndex:0,
			selHouseUnit:1,
			selectedName:'',
			
			mouseX:0,
			mouseY:0,
			districtId:mdistrictId,
			parkId:mparkId,
			token:mtoken,
			districtName:dName,
			
			inputCarNoR:'',
			inputCarNoL:'',
			filterInputCarNosR:[],
			filterInputCarNosL:[],
			mapscaleStyle:{},
			
			currSelParkIndex:0,
			selParkWidth:30,
			selParkHeight:60,
			selParkX:30,
			selParkY:30,
			
        },
        watch: {
            selected: function (value) {
                var sed = false;
                for (var i in value) {
                    sed = true;
                    break;
                }
                app.isSelected = sed;
            },
			inputCarNoR:function(){
			    this.inputCarNofocusR();
			},
			inputCarNoL:function(){
			    this.inputCarNofocusL();
			},
        },
		mounted(){
		},
        methods: {
			inputCarNofocusR(e){
				if(this.$refs.carnameR==document.activeElement){
					this.searchCarNoR(this.inputCarNoR.trim());
				}
			},
			inputCarNofocusL(e){
				if(this.$refs.carnameL==document.activeElement){
					this.searchCarNoL(this.inputCarNoL.trim());
				}
			},
			searchCarNoR:function(value){
			    var that =this;
			    if(!value){
			        app.filterInputCarNosR.length=0;
			        return;
			    }
			    //从服务器返回的车位数据中进行筛选
				app.filterInputCarNosR.length=0;
				app.shapes.forEach(item=>{
					if(item.name.includes(value)){
						app.filterInputCarNosR.push(item);
					}
				});
			},
			searchCarNoL:function(value){
			    var that =this;
			    if(!value){
			        app.filterInputCarNosL.length=0;
			        return;
			    }
			    //从服务器返回的车位数据中进行筛选
				app.filterInputCarNosL.length=0;
				app.cardata.forEach(item=>{
					if(item.name.includes(value)){
						app.filterInputCarNosL.push(item);
					}
				});
			},
			searchCarNoClickR(e,shape){
				if(app.selected.name==undefined){
					let ishave=false;
					let ishide=false;
					for(let i=0,n=app.shapes.length;i<n;i++){
						if(app.shapes[i].name==shape.name){
							ishave=true;
							if(app.shapes[i].show==false){
								ishide=true;
								app.shapes.slice(i,1);
								app.selected.name=shape.name;
								app.selected.sname=shape.name;
								app.selected.price=shape.price;
							}
							break;
						}
					}
					if(ishave){
						if(!ishide){
							alert(shape.name+'已经存在');
						}
					}else{
						app.selected.name=shape.name;
						app.selected.sname=shape.name;
						app.selected.price=shape.price;
					}
				}
				app.inputCarNoR=shape.name;
				app.selectShape(e,shape);
				app.filterInputCarNosR.length=0;
			},
			searchCarNoClickL(e,shape){
				if(app.selected.name==undefined){
					let ishave=false;
					let ishide=false;
					for(let i=0,n=app.shapes.length;i<n;i++){
						if(app.shapes[i].name==shape.name){
							ishave=true;
							if(app.shapes[i].show==false){
								ishide=true;
								app.shapes.slice(i,1);
								app.selected.name=shape.name;
								app.selected.sname=shape.name;
								app.selected.price=shape.price;
							}
							break;
						}
					}
					if(ishave){
						if(!ishide){
							alert(shape.name+'已经存在');
						}
					}else{
						app.selected.name=shape.name;
						app.selected.sname=shape.name;
						app.selected.price=shape.price;
					}
				}
				app.inputCarNoL=shape.name;
				app.selectShape(e,shape);
				app.filterInputCarNosL.length=0;
				app.currSelParkIndex=shape.index;
			},
            cardClick(e) {
				//清除所有元素的选中状态
                if (e.button != 0) {
                    return;
                }
                app.shapes.forEach(item => {
                    item.active = false;
                    item.selectArea = false;
                });
                app.popup.show = false;
                app.selected = {};
				app.inputCarNoR='';
				app.inputCarNoL='';
            },
            selectShape(e, shape) {
				app.inputCarNoR='';
				app.inputCarNoL='';
				app.filterInputCarNosR.splice(0,app.filterInputCarNosR.length);
				app.filterInputCarNosL.splice(0,app.filterInputCarNosL.length);
                this.shapes.forEach(item => {
                    if (!item.selectArea) {
                        item.active = false;
                    }
					if(item.name==shape.name){
						item.active = true;
					}else{
						item.active = false;
					}
                });
				
				this.cardata.forEach(item=>{
					if(item.name==shape.name){
						item.active = true;
						app.inputCarNoR=item.name;
						app.inputCarNoL=item.name;
					}else{
						item.active = false;
					}
				});
                shape.active = true;
				if(shape.type==2){//楼梯口/电梯口
					if(shape.name.indexOf('_')!=-1){
						let ids=shape.name.split("_");
						app.selHouse=parseInt(ids[0]);
						app.selHouseUnit=parseInt(ids[1]);
						for(let i=0,n=app.houseData.length;i<n;i++){
							if(app.houseData[i].id==app.selHouse){
								app.selHouseIndex=i;
								break;
							}
						}
					}
				}
                if (e) {
                    e.preventDefault();
                }
				myConsoleLog(JSON.stringify(shape));
                this.selected = shape;
                this.isSelected = true;
				this.selParkWidth=shape.width;
				this.selParkHeight=shape.height;
				this.selParkX=shape.left;
				this.selParkY=shape.top;
            },
			selectCarShape(e,shape){
				this.cardata.forEach(item=>{
					if(item.name==shape.name){
						item.active = true;
					}else{
						item.active = false;
					}
				});
				this.shapes.forEach(item => {
				    if (!item.selectArea) {
				        item.active = false;
				    }
					if(item.name==shape.name){
						item.active = true;
					}else{
						item.active = false;
					}
				});
				this.selected = shape;
				this.isSelected = true;
				
				this.currSelParkIndex=shape.index;
			},
            showContextmenu: function (e, data) {
                app.popup.left = e.clientX;
                app.popup.top = e.clientY;
                app.popup.show = true;
                app.popup.data = data;
            },
            showCardMenu: function (e) {
                //记录鼠标指针临时位置
				let oDiv = document.getElementById('div1');
				let mousex=event.x-210+$(idCScroll).scrollLeft();
				let mousey=event.y+$(idCScroll).scrollTop();
				if(app.currSelParkIndex<app.cardata.length){
					let shape=app.cardata[app.currSelParkIndex];
					app.selectCarShape(null,shape);
					app.addShape(3,mousex/app.layout.scale,mousey/app.layout.scale);
					
					$(idLScroll).scrollTop(app.currSelParkIndex*26);
					$(idRScroll).scrollTop(app.currSelParkIndex*26);
					
					app.cardata[app.currSelParkIndex].add=true;
					app.currSelParkIndex++;
				}
            },
            cardKeyup: function (e) {
                if (e.keyCode == 9) {
                    app.isShiftDown = false;
                }
            },
			selectAll: function () {
				app.shapes.forEach(item => {
					item.active = true;
					item.selectArea = true;
				});
			},
			unselectAll: function () {
				app.shapes.forEach(item => {
					item.active = false;
					item.selectArea = false;
				});
			},
            shapeMove: function (e, data, index) {
                var code = e.keyCode;
                if (e.ctrlKey && code == 65) {
                    this.selectAll();
                    return;
                }
                if (e.ctrlKey && code == 68) {
                    this.unselectAll();
                    return;
                }
                var keymaps = {//键盘方向键移动元素
                    37: () => moveLeft(-1),
                    38: () => moveTop(-1),
                    39: () => moveLeft(1),
                    40: () => moveTop(1),
                    9: () => app.isShiftDown = true,
                    8: function () {
                        app.popup.del();
                    },
                };
                var fun = keymaps[code];
                if (fun) {
                    fun.call(data, index);
                }
				e.preventDefault();
            },
            addShape: function (type, x, y) {
                let value = '文本';
				let name='';
				let price=1;
				let status=2;
				let property=0;
				
				x=x-app.selParkWidth/2;
				y=y-app.selParkHeight/2;
				if(Math.abs(app.selParkX-x)<app.selParkWidth){
					x=app.selParkX;
				}
				if(Math.abs(app.selParkY-y)<app.selParkHeight){
					y=app.selParkY;
				}
				
                //1通道,2电梯/楼梯口,3车位,4出口,5入口
				if(type==1){
					value = '#32CD32';
					name=getDtId();
					sname='通道';
				}else if(type==2){
					value = '#00BFFF';
					name=getDtId();
					app.selectedName=name;
					sname="电梯";
					if(app.houseData.length>0){
						name=app.houseData[app.selHouseIndex].id+"_"+app.houseData[app.selHouseIndex].units[0].id+"_"+app.selectedName;
						sname=app.houseData[app.selHouseIndex].name+app.houseData[app.selHouseIndex].units[0].name;
					}else{
						alert('楼和单元数据为空');
					}
				}else if(type==3){
					value = '#FFD700';
					price=app.selected.price;
					property=app.selected.property;
					let shape=null;
					for(let i=0,n=this.shapes.length;i<n;i++){
						if(this.shapes[i].name==app.selected.name){
							shape=this.shapes[i];
							break;
						}
					}
					for(let i=0,n=this.cardata.length;i<n;i++){
						if(this.cardata[i].name==app.selected.name){
							if(!this.cardata[i].show){
								this.cardata[i].show=true;
							}
							break;
						}
					}
					if(shape){
						if(shape.show){
							alert('车位已经存在');
						}else{
							shape.show=true;
							shape.left=x;
							shape.top=y;
							return;
						}
						if(shape.left>1000){
							shape.left=300;
						}
						if(shape.top>710){
							shape.top=300;
						}
						app.selectShape(null, shape);
						return;
					}else{
						status=app.selected.status;
						name=app.selected.name;
					}
					sname=name;
				}else if (type == 4) {//
					value = '#228B22';
					name=getDtId();
					sname='出口';
				}else if (type == 5) {
                    value = '#6B8E23';
					name=getDtId();
					sname='入口';
                }
				
				let data = {
					'price':price,
					'left': x,
					'top': y,
					'width': app.selParkWidth,
					'height': app.selParkHeight,
					'value': value,
					'type': type,
					'deg':0,
					'status':status,
					'active': true,
					'property':property,
					'name':name,
					'sname':sname,
					'show':true,
					'measure':0
				};
                app.selectShape(null, data);
                pushShape(data);
            },
			changeCarNoGround(){
				app.layout.background='url('+this.$refs.carnoground.value+')'
			},
			finishInputGroundW(){
				app.card.width0=this.$refs.groundw.value;
				app.card.width=app.card.width0;
			},
			finishInputGroundH(){
				app.card.height0=this.$refs.groundh.value;
				app.card.height=app.card.height0;
			},
			changeSelHouse(){
				let selHouse=this.$refs.selHouse.value;
				for(let i=0,n=app.houseData.length;i<n;i++){
					if(app.houseData[i].id==selHouse){
						app.selHouseIndex=i;
						app.selHouseUnit=app.houseData[i].units[0].id;
						app.selected.name=app.houseData[i].id+"_"+app.houseData[i].units[0].id;
						app.selected.sname=app.houseData[i].name+app.houseData[i].units[0].name;
						break;
					}
				}
			},
			changeSelHouseUnit(){
				let unit=this.$refs.selHouseUnit.value;
				app.selected.name=app.houseData[app.selHouseIndex].id+"_"+unit;
				for(let i=0,n=app.houseData[app.selHouseIndex].units.length;i<n;i++){
					if(app.houseData[app.selHouseIndex].units[i].id==unit){
						app.selected.sname=app.houseData[app.selHouseIndex].name+app.houseData[app.selHouseIndex].units[i].name;
						break;
					}
				}
			},
			onSaveMapData(){//保存地图图片，不进行数据校验
				let mapData={};
				let mapTile=[];
				mapData.map={
						'districtId':app.districtId,
						'parkId':app.parkId,
						'name':app.layout.mapName,
						'mw':app.card.width,
						'mh':app.card.height,
						'ground':app.layout.background.replace("url(","").replace(")","")
					};
				let mapItem={};
				let mapItemname=[];
				app.shapes.forEach(item=>{
					if(item.show){
						if(item.price==0){
							item.price=1;
						}
						if(mapItemname.indexOf(item.name)==-1){
							mapItem={
								'name':item.name,
								'left':(item.left-app.layout.left),
								'top':item.top,
								'width':item.width,
								'height':item.height,
								'price':item.price,
								'type':item.type,
								'deg':item.deg,
								'status':item.status,
								'property':item.property,//0无产权，1有产权
								'measure':item.measure,
								'sname':item.sname
							};
						}
					}
					mapTile.push(mapItem);
				})
				mapData.tile=mapTile;
				postSaveMapData(mapData);
			},
			onPublishMapData(){
				app.shapes.forEach(item=>{
					item.active=false;
				});
				myConsoleLog('districtId='+app.districtId);
				if(app.districtId==undefined){
					alert('未获取到小区编号');
					return;
				}
				if(app.parkId==null){
					alert('未获取到车库的编号');
					return;
				}
				for(let i=0,n=app.shapes.length;i<n;i++){
					if(app.shapes[i].show){
						if(app.shapes[i].name==''||app.shapes[i].name=='车位'){
							app.shapes[i].active=true;
							if(app.shapes[i].type==1){//1通道,2电梯/楼梯口,3车位,4出口,5入口
								alert('请输入通道编号');
							}else if(app.shapes[i].type==2){
								alert('请输入电梯/楼梯口编号');
							}else if(app.shapes[i].type==3){
								alert('请输入车位编号');
							}else if(app.shapes[i].type==4){
								alert('请输入出口编号');
							}else if(app.shapes[i].type==5){
								alert('请输入入口编号');
							}
							return;
						}
					}
				}
				let mapData={};
				let mapTile=[];
				mapData.map={
						'districtId':app.districtId,
						'parkId':app.parkId,
						'name':app.layout.mapName,
						'mw':app.card.width,
						'mh':app.card.height,
						'ground':app.layout.background.replace("url(","").replace(")","")
					};
				let mapItem={};
				let mapItemname=[];
				app.shapes.forEach(item=>{
					if(item.show){
						if(item.price==0){
							item.price=1;
						}
						if(mapItemname.indexOf(item.name)==-1){
							mapItem={
								'name':item.name,
								'left':(item.left-app.layout.left),
								'top':item.top,
								'width':item.width,
								'height':item.height,
								'price':item.price,
								'type':item.type,
								'deg':item.deg,
								'status':item.status,
								'property':item.property,//0无产权，1有产权
								'measure':item.measure,
								'sname':item.sname
							};
						}
					}
					mapTile.push(mapItem);
				})
				mapData.tile=mapTile;
				postMapData(mapData);//将编辑好的车位平面图数据提交给服务器
			},
			selectCarData(e){//左侧搜索
				for(let i=0,n=this.cardata.length;i<n;i++){
					if(this.cardata[i].name==app.selectCarName){
						app.selectCarShape(null,this.cardata[i]);
						return;
					}
				}
			},
			selectElement(e){//右侧搜索
				for(let i=0,n=this.shapes.length;i<n;i++){
					if(this.shapes[i].name==app.selectElementName){
						app.selectShape(null,this.shapes[i]);
						return;
					}
				}
			},
			updateXY:function(event){
			    this.mouseX=event.x-210+$(idCScroll).scrollLeft();
			    this.mouseY=event.y+$(idCScroll).scrollTop();
			},
			upLoadImg:function(event){
				var $file = event.currentTarget;
				var files = $file.files;
				
				javascript:$('input[name=\'fileName\']').val(files[0].name);
				var formData = new FormData();
				formData.append("file", files[0]);
				$.ajax({
				        url: HttpUrl+'/ali/oss/backstage/v1/',
				        type: 'POST',
				        data:formData,
						dataType: 'json',
				        processData: false,
						contentType : false,
						beforeSend:function(){
							myConsoleLog("正在进行，请稍候");
						},
						success : function(res) {
							myConsoleLog('上传车位平面图的背景图');
							myConsoleLog(JSON.stringify(res));
							if(res.status==200){
								app.layout.background='url('+`http://obts.jinwei.net/${res.data.current}`+')';
								var image = new Image();
								image.src='http://obts.jinwei.net/'+res.data.current;
								image.onload = function() {
									app.card.width=image.width;
									app.card.height=image.height;
									myConsoleLog('w='+app.card.width+',h='+app.card.height);
								}
							}else{
								alert(res.error.msg);
							}
						},
						error : function(responseStr) {
							myConsoleLog("error");
						}
				    })
			},
            changeDeg(e){
				app.selected.deg=e.target.value;
                let divshapes=document.getElementsByClassName("shape");
				for(let i=0,n=divshapes.length;i<n;i++){
					if(divshapes[i].getAttribute("data-id")==app.selected.id){
						divshapes[i].style.transform = 'rotate('+ -app.selected.deg +'deg)';
						break;
					}
				}
            },
			clickEnlarge(e){
				app.layout.scale+=0.5;
			},
			clickNarrow(e){
				app.layout.scale-=0.5;
			},
			clickPreview(e){
				//预览
				let bigscreen=PreviewUrl+"?districtName="+app.districtName+"&districtId="+app.districtId+"&parkId="+app.parkId+"&token="+app.token;
				window.open(bigscreen);
			},
			centerMousedown(ev){
				let oDiv = document.getElementById('idCScroll');
				let isPressed=true;
				let disX= ev.clientX;
				let disY= ev.clientY;
				document.onmousemove = function (ev) {
					//解决兼容问题，ie8
					if(isPressed){
						ev = ev || window.event;
						let l = ev.clientX - disX;
						let t = ev.clientY - disY;
						if(Math.abs(l)>30){
							oDiv.scrollLeft= l;
						}
						if(Math.abs(t)>30){
							oDiv.scrollTop=t;
						}
					}
				}
				document.onmouseup = function () {
					isPressed=false;
					disX= ev.clientX;
					disY= ev.clientY;
					document.onmousemove = null;
					document.onmouseup = null;
				}
				return false;
			}
        }
    });
}

function initLayout() {
    function handlerLayout() {
        var size = getSize();
        app.layout.width = size.width;
        app.layout.height = size.height;
        app.layout.center = size.width - app.layout.left - app.layout.right - 22 - (app.layout.margin * 2);
    }
    handlerLayout();
    window.onresize = handlerLayout;
}
function getSize() {
    return {
        width: document.documentElement.clientWidth || document.body.clientWidth,
        height: document.documentElement.clientHeight || document.body.clientHeight
    };
}
function getDtId() {
	var d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth() + 1;
	var date = d.getDate();
	var day = d.getDay();
	var hours = d.getHours();
	var minutes = d.getMinutes();
	var seconds = d.getSeconds();
	var ms = d.getMilliseconds();
	year = (year + "").substring(2);
	if (month <= 9)
	month = "0" + month;
	if (date <= 9)
	date = "0" + date;
	if (hours <= 9)
	hours = "0" + hours;
	if (minutes <= 9)
	minutes = "0" + minutes;
	if (seconds <= 9)
	seconds = "0" + seconds;
	var id = year + month + date + hours + minutes + seconds + ms;
	return id;
}
//---------------------------------服务器端接口----------------------------

function getServerCarData(){//-----------------获取后台录入的车位数据0618
	// let temp=[
	//   {"name": "s1", "left": 162, "top": 135, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s2", "left": 266, "top": 139, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s3", "left": 268, "top": 190, "width": 61, "height":26, "price":14,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s4", "left": 160, "top": 161, "width": 61, "height":26, "price":14,"status":1,"type":3,"deg":0,"property":1,'measure':12},
	//   {"name": "s5", "left": 160, "top": 186, "width": 61, "height":26, "price":14,"status":1,"type":3,"deg":0,"property":1,'measure':12},
	//   {"name": "s6", "left": 161, "top": 212, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':8},
	//   {"name": "s7", "left": 161, "top": 236, "width": 61, "height":26, "price":15,"status":6,"type":3,"deg":0,"property":1,'measure':8},
	//   {"name": "s8", "left": 162, "top": 261, "width": 61, "height":26, "price":15,"status":1,"type":3,"deg":0,"property":1,'measure':9},
	//   {"name": "s9", "left": 160, "top": 289, "width": 61, "height":26, "price":16,"status":1,"type":3,"deg":0,"property":1,'measure':5},
	//   {"name": "s10", "left": 159, "top": 314, "width": 61, "height":26, "price":16,"status":1,"type":3,"deg":0,"property":1,'measure':9},
	//   {"name": "s11", "left": 160, "top": 339, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s12", "left": 162, "top": 366, "width": 61, "height":26, "price":20,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s13", "left": 161, "top": 392, "width": 61, "height":26, "price":21,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s14", "left": 161, "top": 416, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':10},
	//   {"name": "s15", "left": 163, "top": 442, "width": 61, "height":26, "price":22,"status":1,"type":3,"deg":0,"property":1,'measure':15},
	//   {"name": "s16", "left": 163, "top": 471, "width": 61, "height":26, "price":13,"status":2,"type":3,"deg":0,"property":1,'measure':15},
	//   {"name": "s17", "left": 162, "top": 494, "width": 61, "height":26, "price":25,"status":2,"type":3,"deg":0,"property":1,'measure':12},
	//   {"name": "s18", "left": 780, "top": 310, "width": 61, "height":26, "price":26,"status":3,"type":3,"deg":0,"property":1,'measure':12},
	//   {"name": "s19", "left": 782, "top": 336, "width": 61, "height":26, "price":20,"status":1,"type":3,"deg":0,"property":1,'measure':3},
	//   {"name": "s20", "left": 782, "top": 365, "width": 61, "height":26, "price":13,"status":1,"type":3,"deg":0,"property":1,'measure':5},
	//   {"name": "s21", "left": 781, "top": 391, "width": 61, "height":26, "price":18,"status":1,"type":3,"deg":0,"property":1,'measure':5},
	//   {"name": "s22", "left": 781, "top": 416, "width": 61, "height":26, "price":16,"status":1,"type":3,"deg":0,"property":1,'measure':6},
	//   {"name": "s23", "left": 780, "top": 442, "width": 61, "height":26, "price":13,"status":4,"type":3,"deg":0,"property":1,'measure':8},
	//   {"name": "s24", "left": 782, "top": 470, "width": 61, "height":26, "price":17,"status":1,"type":3,"deg":0,"property":1,'measure':9},
	//   {"name": "s25", "left": 780, "top": 497, "width": 61, "height":26, "price":17,"status":3,"type":3,"deg":0,"property":1,'measure':9},
	// ];
	// let index=0;
	// let tempData=[];
	// temp.forEach(item=>{
	// 	tempData.push({'index':index,'name':item.name,'height':item.hight,'measure':item.measure,'width':item.width,'price':item.price,'status':1});
	// 	index++;
	// })
	// app.cardata=tempData;
	

	let cardata=[];//通过接口从服务器端获取的后台录入的车位数据
	$.ajax({
		'url': HttpUrl+'/parkingspace/forWoner/Cside/lot/list/v1/'+encodeURI(JSON.stringify({'garageId':app.parkId})),
		'type': 'get',
		'headers': {'operator': operator,'token':token,'Content-Type':'application/json'},
		'processData': false,
		'dataType': 'json',
		'success': function (res) {
			myConsoleLog('获取后台录入的车位数据');
			myConsoleLog(JSON.stringify(res));
			if(res.status==200){
				let index=0;
				res.data.forEach(item=>{
					cardata.push({'index':index,'name':item.name,'height':item.hight,'measure':item.measure,'width':item.width,'price':item.price,'status':item.state.value});
					index++;
				})
				app.cardata=cardata;
			}else{
				// myConsoleLog(JSON.stringify(res));
				alert(res.error.msg);
			}
		},
		'error': function(xhr, errorType, error) {
			alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
		}
	});
}

function getServerHouseUnit(){//xxxxxxxxxxxxxxxxxxxxxx获取后台录入的楼号和单元号
	$.ajax({//根据车库ID获取后台录入的楼号和单元号
		'url': HttpUrl+'/district/buildingAndUnit/select/byGarage/s/v1/'+encodeURI(JSON.stringify({'districtId':app.districtId,'id':app.parkId})),
		'type': 'get',
		'headers': {'operator': operator,'token':token,'Content-Type':'application/json'},
		'processData': false,
		'dataType': 'json',
		'success': function (res) {
			myConsoleLog('获取后台录入的楼号和单元号');
			myConsoleLog(JSON.stringify(res));
			if(res.status==200){
				app.houseData.splice(0,app.houseData.length);
				app.houseData=res.data;
			}else{
				alert(res.error.msg);
			}
		},
		'error': function(xhr, errorType, error) {
			alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
		}
	});
}

function postMapData(mapData){//xxxxxxx编辑好的车位平面图数据提交给服务器
	myConsoleLog('编辑好的车位平面图数据提交给服务器');
	myConsoleLog(JSON.stringify(mapData));
	$.ajax({
		'url': HttpUrl+'/administrativeunit/map/data/v1/',
		'type': 'post',
		'headers': {'operator': operator,'token':token,'Content-Type':'application/json'},
		'processData': false,
		'dataType': 'json',
		'data':JSON.stringify(mapData),
		'success': function (data) {
			if(data.status==200){
				alert('数据发布成功！');
			}else{
				myConsoleLog(JSON.stringify(data));
			}
		},
		'error': function(xhr, errorType, error) {
			alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
		}
	});
}

function postSaveMapData(mapData){
	myConsoleLog('未编写完的车位平面图数据提交给服务器---保存');0
	myConsoleLog(JSON.stringify(mapData));
	$.ajax({
		'url': HttpUrl+'/administrativeunit/map/data/temp/v1',
		'type': 'post',
		'headers': {'operator': operator,'token':token,'Content-Type':'application/json'},
		'processData': false,
		'dataType': 'json',
		'data':JSON.stringify(mapData),
		'success': function (data) {
			if(data.status==200){
				alert('数据保存成功！');
			}else{
				myConsoleLog(JSON.stringify(data));
			}
		},
		'error': function(xhr, errorType, error) {
			alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
		}
	});
}
function getUploadMapData(){//------------------------获取之前上传的车位平面图数据06-18
	$.ajax({
		'url': HttpUrl+'/administrativeunit/map/data/v1/'+encodeURI(JSON.stringify({'parkId':app.parkId})),
		'type': 'get',
		'headers': {'operator': operator,'token':token,'Content-Type':'application/json'},
		'processData': false,
		'dataType': 'json',
		'success': function (data) {
			myConsoleLog('获取之前上传的车位平面图数据');
			myConsoleLog(JSON.stringify(data));
			if(data.status==200){
				app.districtId=data.data.map.districtId;
				app.parkId=data.data.map.parkId;
				app.layout.mapName=data.data.map.name;
				app.card.name=data.data.map.name;
				app.card.width=data.data.map.mw;
				app.card.height=data.data.map.mh;
				app.card.width0=data.data.map.mw;
				app.card.height0=data.data.map.mh;
				app.layout.background='url('+data.data.map.ground+')';
				
				let names=[];
				data.data.tile.forEach((item,index)=>{
					if(names.indexOf(item.name)==-1){
						item.show=true;
						item.index=(index+1);
						item.id=item.name;
						item.sname=item.sname;
						item.left=app.layout.left+item.left;
						let value;
										
						if(item.type==1){//type 1通道,2电梯/楼梯口,3车位,4出口,5入口
							value = '#32CD32';
						}else if(item.type==2){
							value = '#00BFFF';
							let temp=getHouseUnitFromHouseData(item.name);
							item.sname=temp[1]+temp[0];
						}else if(item.type==3){
							value = '#FFD700';
						}else if (item.type == 4) {
							value = '#228B22';
						}else if (item.type == 5) {
							value = '#6B8E23';
						}
						let sname='';
						if(item.sname==''){
							sname=item.name;
						}else{
							sname=item.sname;
						}
						let data = {
							'price':item.price,
							'left': item.left,
							'top': item.top,
							'width': item.width,
							'height': item.height,
							'value': value,
							'type': item.type,
							'deg':item.deg,
							'status':item.status,
							'active': true,
							'property':item.property,
							'name':item.name,
							'sname':sname,
							'show':true,
							'measure':item.measure,
							'index':item.index
						};
						app.selectShape(null, data);
						pushShape(data);
						names.push(item.name);
					}
				});
			}else{
			}
		},
		'error': function(xhr, errorType, error) {
			alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
		}
	});
}
function getHouseUnitFromHouseData(houseunit){
	let data=houseunit.split('_');
	let houseUnit=[];
	app.houseData.forEach(item=>{
		if(item.id==data[0]){//楼号ID对应上
			item.units.forEach(unit=>{
				if(unit.id==data[1]){
					houseUnit.push(unit.name)
				}
			})
			houseUnit.push(item.name)
		}
	});
	return houseUnit;
}

function myConsoleLog(msg){
	console.log(msg);
}